package GUILayer;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import CtrLayer.ProductCtr;
import ModelLayer.Product;

public class ProductTableModel  extends AbstractTableModel {
	private ProductCtr prodCtr;
	private ArrayList<Product> li = new ArrayList<Product>();
	private String[] columnNames = { "ID", "Type", "Name",
			"Purchase Price", "Sales Price", "Rent Price",
			"Made in",  "Minimum Stock", "Stock", "Supplier"};
	
	public ProductTableModel(){
		prodCtr = new ProductCtr();
	}
	public void populateList()
	{
		ArrayList<Product> tmpList = prodCtr.listProducts();
		System.out.println(tmpList.size());
		if(tmpList != null)
		{
			this.li.clear();
			this.li = tmpList;
		}
		fireTableDataChanged();
	}

	public void populateList(String filter) {
		ArrayList<Product> tmpList = prodCtr.listProducts(filter);
		System.out.println(tmpList.size());
		if(tmpList != null)
		{
			this.li.clear();
			this.li = tmpList;
		}
		fireTableDataChanged();
	}
	
	@Override
	public String getColumnName(int columnIndex){
		return columnNames[columnIndex];
	}

	@Override     
	public int getRowCount() {
		return li.size();
	}

	@Override        
	public int getColumnCount() {
		return 10;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Product p = li.get(rowIndex);
		switch (columnIndex) {
		case 0: 
			return p.getProductId();
		case 1:
			return p.getClass().getSimpleName().toString();
		case 2:
			return p.getName();
		case 3:
			return p.getPurchasePrice();
		case 4:
			return p.getSalesPrice();
		case 5:
			return p.getRentPrice();
		case 6:
			return p.getCountryOfOrigin();
		case 7:
			return p.getMinStock();
		case 8:
			return p.getStockAmount();
		case 9:
			return p.getSupplier().getName();		
		}
		return null;
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex){
		switch (columnIndex){
		case 0:
			return Integer.class;
		case 1:
			return String.class;
		case 2:
			return String.class;
		case 3:
			return Double.class;
		case 4:
			return Double.class;
		case 5:
			return Double.class;
		case 6:
			return String.class;
		case 7:
			return Integer.class;
		case 8:
			return Integer.class;
		case 9:
			return String.class;
		}
		return null;
	}
}